package com.plian.system.service.pf.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.system.entity.pf.AppraisalIndex;
import com.plian.system.entity.pf.TaskIndicators;
import com.plian.system.entity.pf.TaskIndicatorsRq;
import com.plian.system.mapper.pf.TaskIndicatorsMapper;
import com.plian.system.mapper.pf.TaskIndicatorsRqMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.pf.IAppraisalIndexService;
import com.plian.system.service.pf.ITaskIndicatorsRqService;
import com.plian.system.service.pf.ITaskIndicatorsService;
import com.plian.system.vo.pf.TaskIndicatorsRqVO;
import com.plian.system.vo.pf.TaskIndicatorsVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Take-off
 * @Description: TODO 任期经营业绩考核指标表-任务绩效 服务实现类
 * @Date: 2022/12/14 9:29 AM
 * @Version: 1.0
 **/
@Service
@AllArgsConstructor
public class TaskIndicatorsRqServiceImpl extends BaseServiceImpl<TaskIndicatorsRqMapper, TaskIndicatorsRq> implements ITaskIndicatorsRqService {

    private IAppraisalIndexService appraisalIndexService;

    @Override
    public boolean deleteByParentId(List<Long> ids) {
        LambdaQueryWrapper<TaskIndicatorsRq> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(TaskIndicatorsRq::getBaseBusinessIndicatorsId, ids);
        List<TaskIndicatorsRq> taskIndicators = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(taskIndicators)){
            return deleteLogic(taskIndicators.stream().map(TaskIndicatorsRq::getId).collect(Collectors.toList()));
        }
        return true;
    }

    @Override
    public void setTaskName(List<TaskIndicatorsRqVO> taskIndicatorsRqVOS){
        List<AppraisalIndex> list = appraisalIndexService.list(new LambdaQueryWrapper<AppraisalIndex>()
                .in(AppraisalIndex::getId, taskIndicatorsRqVOS.stream().map(TaskIndicatorsRqVO::getIndicatorsCategoryId).collect(Collectors.toList())));
        if (CollectionUtil.isNotEmpty(list)){
            taskIndicatorsRqVOS.forEach(m -> {
                List<AppraisalIndex> indices = list.stream().filter(index -> m.getIndicatorsCategoryId().equals(index.getId())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(indices)){
                    m.setIndicatorsCategoryName(indices.get(0).getName());
                }
            });
        }
    }
}
